<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;
use util\Tree;


/**
 * 视频分类 模型
 * @package app\user_center\model
 */
class VideoPlayClass extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_class';
	
	/**
     * 获取树形节点
     * @param int $id 需要隐藏的节点id
     * @param string $default 默认第一个节点项，默认为“顶级节点”，如果为false则不显示，也可传入其他名称
     * @param string $module 模型名
     * @author shiqiang <976162041@qq.com>	
     * @return mixed
     */
    public static function getMenuTree($id = 0, $default = '')
    {
        $result[0] = '顶级节点';
        $where = [
            ['status', 'egt', 0]
        ];
		
		// 排除指定节点及其子节点
        if ($id !== 0) {
            $hide_ids = array_merge([$id], self::getChildsId($id));
            $where[]  = ['id', 'not in', $hide_ids];
        }

        // 获取节点
        $menus = Tree::toList(self::where($where)->order('sort asc,pid,id')->column('id,pid,title'));
        foreach ($menus as $menu) {
            $result[$menu['id']] = $menu['title_display'];
        }

        // 设置默认节点项标题
        if ($default != '') {
            $result[0] = $default;
        }

        // 隐藏默认节点项
        if ($default === false) {
            unset($result[0]);
        }

        return $result;
    }
	
	/**
     * 获取所有子节点id
     * @param int $pid 父级id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public static function getChildsId($pid = 0)
    {
        $ids = self::where('pid', $pid)->column('id');
        foreach ($ids as $value) {
            $ids = array_merge($ids, self::getChildsId($value));
        }
        return $ids;
    }
	
	/**
     * 获取所有子节点id和name
     * @param int $pid 父级id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public static function getChildsInfo($pid = 0)
    {
        $ids = self::where('pid', $pid) -> column('id,title');
        foreach ($ids as $key => $value) {
            $ids = $ids + self::getChildsInfo($key);
        }
        return $ids;
    }
	
	/**
     * 获取所有分类
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getClass(){
		$where = [
            ['status', 'egt', 0]
        ];
		$menus = Tree::toList(self::where($where)->order('sort,pid,id')->column('id,pid,title,sort'));
		return $menus;
	}
	
	/**
     * 获取所有父节点id
     * @param int $id 节点id
     * @author shiqiang
     * @return array
     */
    public static function getParentsId($id = 0)
    {
        $pid  = self::where('id', $id)->value('pid');
        $pids = [];
        if ($pid != 0) {
            $pids[] = $pid;
            $pids = array_merge($pids, self::getParentsId($pid));
        }
		sort($pids);
        return $pids;
    }

    /**
     * 添加数据
     * @param int $pid 		上级id
     * @param string $title 	分类标题
     * @param int $sort 	排序
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function addData($pid, $title, $sort){
        $this -> pid            = $pid;
        $this -> title         	= $title;
        $this -> sort         	= $sort;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success' , $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 编辑数据
     * @param int $id 		
     * @param int $pid 		上级id
     * @param string $title 	分类标题
     * @param int $sort 	排序
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function editData($id, $pid, $title, $sort){
        $data['pid']     = $pid;
        $data['title']   = $title;
        $data['sort']    = $sort;
        $res = $this -> where('id', $id) -> update($data);
        if($res !== false){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 删除数据
     * @param int $id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $map        = [];
        $map['id']     = $id;
        $res = self::destroy(function($query) use ($map){
            $query->where($map);
        });
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	public function getListByLabelID($pid){
		$map = [];
        $map['pid'] = $pid;
        $list = self::with('VideoPlayClassContact', 'LEFT') -> where($map) -> order('sort' ,'asc') -> select();
        return array_out(200, 'success', $list);
	}
	
	/**
     * 标签关联表 关联载入（一对一）
     */
    public function VideoPlayClassContact(){
        return $this -> hasMany('app\video\model\VideoPlayClassContact','pid','id');
    }
	
	/**
     * 标签 关联载入（一对一）
     */
    public function getVideoPlayClassLabel(){
        return $this -> hasOne('app\video\model\VideoPlayClassLabel','id','label_id');
    }
	
	/**
     * 视频关联 关联载入（一对一）
     */
    public function getVideoPlay(){
        return $this -> hasMany('app\video\model\VideoPlay','video_classid','id');
    }
}
